//
//  MoveCarSettingTImeV.m
//  carcareIOS
//
//  Created by 王然 on 16/9/20.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MoveCarSettingTimeV.h"
#define Bottom_BackV_Height (44 + 45 + 180 + (49 + 49) * __MAIN_RATIO_375)

#define Word_Blue_Color (COLOR_HEXSTRING(@"#1f90e5"))
#define Word_Gray_Color (COLOR_HEXSTRING(@"#949494"))

typedef NS_ENUM(NSInteger , Click_Type)
{
    Click_Type_Sart = 70,// 点击开始时间的time
    Click_Type_End,// 点击结束时间的time
};
@interface MoveCarSettingTimeV()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic , strong) NSMutableArray * hours;//小时数组

@property (nonatomic , strong) NSMutableArray * minutes;//分钟数组

@property (nonatomic , readonly) NSArray * btnTexts;//起始时间按钮的文字

@property (nonatomic , strong) UIView * backV;

@property (nonatomic , strong) UIButton * startTimeBtn;

@property (nonatomic , strong) UIButton * endTimeBtn;

@property (nonatomic , copy) NSString * startTimeStr;

@property (nonatomic , copy) NSString * endTimeStr;

@property (nonatomic , strong) UIPickerView * timePickView;

@property (nonatomic , assign) Click_Type clickType;

@property (nonatomic , copy) void(^sureClick)(NSString * startTime , NSString * endTime);

@end
@implementation MoveCarSettingTimeV

- (instancetype)init{
    self = [super initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT)];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];

        [self loadData];
        [self createUI];
        
    }
    return self;
}
- (void)loadData
{
    self.hours = [NSMutableArray arrayWithCapacity:24];
    self.minutes = [NSMutableArray arrayWithCapacity:60];
    for (int index = 0; index < 60; index ++) {
        NSString * smallStr = (index < 10 ? [NSString stringWithFormat:@"0%zd",index]:[NSString stringWithFormat:@"%zd",index]);
        if (index < 24) {
            [self.hours addObject:smallStr];
        }
        [self.minutes addObject:smallStr];
    }
}
- (void)createUI
{
    self.backV = [[[UIView viewWithFrame:CGRectMake(0, __MAIN_HEIGHT - Bottom_BackV_Height, __MAIN_WIDTH, Bottom_BackV_Height)] setupOnView:self] resetBackgroundColor:[UIColor whiteColor]];
     __weak __typeof(self) wself = self;
    UIView * headV = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 44)] setupOnView:self.backV] resetBackgroundColor:[Theme backGroundColor]];
    UIButton * cancelBtn = [[UIButton buttonWithFrame:CGRectMake(0, 0, 50, headV.height) title:@"取消" textColor:COLOR_HEXSTRING(@"#1f90e5") font:FONT_DEFAULT_Light(15) click:^{
        [wself animationHidden:YES];
    }] setupOnView:headV];
    
    [[UIButton buttonWithFrame:CGRectMake(__MAIN_WIDTH - cancelBtn.width, 0, cancelBtn.width, cancelBtn.height) title:@"完成" textColor:COLOR_HEXSTRING(@"#1f90e5") font:FONT_DEFAULT_Light(15) click:^{
        if(wself.sureClick)wself.sureClick(wself.startTimeStr , wself.endTimeStr);
        [wself animationHidden:YES];
    }] setupOnView:headV];
    [[[UILabel labelWithFrame:CGRectMake(cancelBtn.right, 0, headV.width - cancelBtn.width * 2, cancelBtn.height) font:FONT_DEFAULT_Light(17) text:@"勿扰时段" textColor:COLOR_HEXSTRING(@"#191919")] setupOnView:headV] labelResetTextAlignment:NSTextAlignmentCenter];
    
    CGFloat labelX = 14 * __MAIN_RATIO_375;
    CGFloat labelW = (__MAIN_WIDTH - labelX * 2) / 2;
    self.startTimeBtn = [[[[[UIButton buttonWithFrame:CGRectMake(labelX + 1, headV.bottom + 49 * __MAIN_RATIO_375, labelW , 45) title:@"" textColor:Word_Blue_Color font:FONT_DEFAULT_Light(17) click:^{
        [wself clickOnTimeBtn:0];
    }] setupOnView:self.backV] resetBorderWidth:1 borderColor:Word_Blue_Color] resetCornerRadius:1] resetBackgroundColor:[UIColor whiteColor]];
    
    self.endTimeBtn = [[[[[UIButton buttonWithFrame:CGRectMake(self.startTimeBtn.right - 1, headV.bottom + 49 * __MAIN_RATIO_375, labelW, 45) title:@"" textColor:Word_Gray_Color font:FONT_DEFAULT_Light(17) click:^{
        [wself clickOnTimeBtn:1];
    }] setupOnView:self.backV] resetBorderWidth:1 borderColor:Word_Gray_Color] resetCornerRadius:1]resetBackgroundColor:[UIColor whiteColor]];
    
    self.timePickView = [[[UIPickerView alloc]initWithFrame:CGRectMake(0, self.endTimeBtn.bottom + 49 * __MAIN_RATIO_375, __MAIN_WIDTH, 180)] setupOnView:self.backV];
    self.timePickView.dataSource = self;
    self.timePickView.delegate = self;
    self.timePickView.backgroundColor = [UIColor clearColor];
    
}
- (void)reloadModel:(MoveCarSettingModel *)model click:(void(^)(NSString * startTime, NSString * endTime))click
{
    
    self.startTimeStr = model.startTime;
    self.endTimeStr = model.endTime;
    
    if(click) self.sureClick = click;
    [self clickOnTimeBtn:0];
}
- (void)clickOnTimeBtn:(NSInteger)index
{
    
    [self resetBtnColor:index];
    
    if (index == 0) {
        self.clickType = Click_Type_Sart;
    }else
    {
        self.clickType = Click_Type_End;
    }
    
    NSArray * timeArr = [(self.clickType == Click_Type_Sart ? self.startTimeStr : self.endTimeStr) componentsSeparatedByString:@":"];
    [self.timePickView selectRow:[self.hours indexOfObject:timeArr[0]] inComponent:0 animated:NO];
    if(timeArr.count > 1)[self.timePickView selectRow:[self.minutes indexOfObject:timeArr[1]] inComponent:1 animated:NO];

    
}
#pragma mark - pickview Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        
        return self.hours.count;
    }
    else
    {
        return self.minutes.count;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        
        return __MAIN_WIDTH / 2;
    }else
    {
        
        return __MAIN_WIDTH / 2;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        
        if(self.clickType > 0)
        {
            NSInteger selectOneRow = [self.timePickView selectedRowInComponent:0];
            if (self.clickType == Click_Type_Sart) {
                self.startTimeStr = [NSString stringWithFormat:@"%@:%@",self.hours[selectOneRow],[self getMinuteStr]];
            }else if(self.clickType == Click_Type_End)
            {
                self.endTimeStr = [NSString stringWithFormat:@"%@:%@",self.hours[selectOneRow],[self getMinuteStr]];
            }
        }
        return self.hours[row];
        
    }else
    {
        NSInteger selectTwoRow = [self.timePickView selectedRowInComponent:1];
        if(self.clickType > 0)
        {
            if (self.clickType == Click_Type_Sart) {
                self.startTimeStr = [NSString stringWithFormat:@"%@:%@",self.hours[[self.timePickView selectedRowInComponent:0]],self.minutes[selectTwoRow]];
            }else if(self.clickType == Click_Type_End)
            {
                self.endTimeStr = [NSString stringWithFormat:@"%@:%@",self.hours[[self.timePickView selectedRowInComponent:0]],self.minutes[selectTwoRow]];
            }
        }
        return self.minutes[row];
    }
}
//获取分钟的值
- (NSString *)getMinuteStr
{
    NSArray * timeArr = [(self.clickType == Click_Type_Sart ? self.startTimeStr : self.endTimeStr) componentsSeparatedByString:@":"];
    return (timeArr.count > 1 ? timeArr[1] : @"00");
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        [self.timePickView selectRow:0 inComponent:1 animated:YES];
        [self.timePickView reloadComponent:1];
    }
    
}
- (void)resetBtnColor:(NSInteger)index
{
    [self.startTimeBtn resetBorderWidth:1 borderColor:(index == 0 ? Word_Blue_Color : Word_Gray_Color)];
    [self.startTimeBtn setTitleColor:(index == 0 ? Word_Blue_Color : Word_Gray_Color) forState:UIControlStateNormal];
    [self.endTimeBtn resetBorderWidth:1 borderColor:(index == 1 ? Word_Blue_Color : Word_Gray_Color)];
    [self.endTimeBtn setTitleColor:(index == 1 ? Word_Blue_Color : Word_Gray_Color) forState:UIControlStateNormal];
    [self.backV bringSubviewToFront:(index == 0 ? self.startTimeBtn : self.endTimeBtn)];
}
- (void)animationHidden:(BOOL)hidden
{
    __weak __typeof(self) wself = self;
    [NSTimer scheduledTimerWithTimeInterval:0.3 block:^{
        [wself setHidden:hidden];
    } repeats:NO];
    
}
- (void)setStartTimeStr:(NSString *)startTimeStr
{
    _startTimeStr = startTimeStr;
    [self.startTimeBtn setTitle:[NSString stringWithFormat:@"%@%@",self.btnTexts[0],startTimeStr] forState:UIControlStateNormal];
}
- (void)setEndTimeStr:(NSString *)endTimeStr
{
    _endTimeStr = endTimeStr;
    [self.endTimeBtn setTitle:[NSString stringWithFormat:@"%@%@",self.btnTexts[1],endTimeStr] forState:UIControlStateNormal];
}
- (NSArray *)btnTexts
{
    return @[@"从 ",@"至 "];
}
@end
